package com.yunxiangyue

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.yunxiangyue.app.R
import okhttp3.Call
import okhttp3.Callback
import okhttp3.FormBody
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import org.json.JSONArray
import java.io.IOException

//import cn.edu.sicnu.lgy.finalwork.info_user.user_info

class MainActivity : AppCompatActivity(), View.OnClickListener {
    lateinit var  textView:TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main3)

        val btn = findViewById<Button>(R.id.btn)
        textView = findViewById(R.id.text)
        btn.setOnClickListener(this)

    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.btn -> {
                val URL = "http://172.20.10.13:8080/tomcat/servlet"
                Thread{
                    val client = OkHttpClient()
                    //封装post请求
                    val requestBody = FormBody.Builder()
                        .add("flag","true")
                        .build()
                    val request = Request.Builder()
                        .url(URL)
                        .addHeader("Connection", "close")
                        .post(requestBody)
                        .build()
                    //同步执行获取服务器端的响应数据
                    client.newCall(request).enqueue(object :
                    Callback{
                        override fun onFailure(call: Call, e: IOException) {
                            Log.d("ning","请求失败")
                            e.printStackTrace()


                        }

                        override fun onResponse(call: Call, response: Response) {
                            if(response.isSuccessful){
                                val content = response.body?.string()
                                Log.d("ning",content.toString())
                                runOnUiThread{
                                    textView.setText(content)
                                    val jsonArray = JSONArray(content)
                                    for(i in 0 until jsonArray.length()){
                                        val jsonObject = jsonArray.getJSONObject(i)
                                        val name = jsonObject.getString("name")
                                        val age = jsonObject.getString("age")
                                        Log.d("ning","$name"+":"+"$age")
                                        textView.setText("$name"+":"+"$age")
                                    }
                                }
                            }else{
                                Log.d("ning","状态码为${response.code}")

                            }
                        }
                    })
                }.start()
            }
        }
    }


}